<template>
  <!-- 合同新版次添加 -->
    <el-dialog title="添加新版次" :visible.sync="openContractNewAdd" width="600px" append-to-body :destroy-on-close="true" :before-close="beforeClose">
      <el-form ref="formParams" :model="formParams" :rules="rules" label-width="150px" v-show="showSearch" :inline="true">
        <el-form-item label="开始日期" prop="sdate" :rules="rules.sdate">
          <el-date-picker
            v-model="formParams.sdate"
            style="width: 240px"
            value-format="yyyy-MM-dd"
            type="date"
            placeholder="选择开始日期"
            >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="结束日期" prop="edate" :rules="rules.edate">
          <el-date-picker
            v-model="formParams.edate"
            style="width: 240px"
            value-format="yyyy-MM-dd"
            type="date"
            placeholder="选择结束日期"
            >
          </el-date-picker>
        </el-form-item>
         <el-form-item label="选择复制版本" prop="cxx">
            <el-select
                v-model="formParams.cxx"
                placeholder="请选择复制版本"
                clearable
            >
                <el-option
                    v-for="item in contractCurrentVersion"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
                />
            </el-select>
        </el-form-item>
         <el-form-item label="不可创建日期" prop="date1">
            <el-date-picker
            v-model="date1"
            value-format="yyyy-MM-dd"
            type="daterange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            :disabled="true">
            </el-date-picker>
        </el-form-item>
        <el-form-item label="最新版次日期" prop="date2">
            <el-date-picker
            v-model="date2"
            value-format="yyyy-MM-dd"
            type="daterange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            :disabled="true">
            </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="handleCreate('formParams')" v-hasPermi="['zhcloud:sfbz:add']">创 建</el-button>
      </div>
    </el-dialog>
</template>

<script>
import { contractNewVersionCreate, getContractNewVersionAddDetail, getContractCurrentVersion  }  from "@/api/storage/contractFeeStandard";

export default {
  name: "ContractNewAdd",
  data() {
    return {
      showSearch:true,
      //弹窗状态
      openContractNewAdd: false,
      //获取当前版次
      getContractCurrentVersionLoading: true,
      //不可创建日期
      date1:[],
      //最新版次日期
      date2:[],
      //参数
      queryParams:{
        csuid:"",
        cx:"",
        s2:"",
        s1:""
      },
      //表单参数
      formParams: {
      	count1: 0,
        csuid: "",
        cx0: 0,
        cxx: "",
        e1: "",
        e2: "",
        edate: "",
        s1: "",
        s2: "",
        sdate: ""
      },
      //当前版次参数
      currentVersionQuery: {
        csuid:""
      },
      rules: {
        csbh: { required: true, message: "合同编号不能为空", trigger: "blur" },
        sdate: { required: true, message: "首次开始日期不能为空", trigger: "blur" },
        edate: { required: true, message: "首次结束日期不能为空", trigger: "blur" },
      },
      contractNewVersionAddDetail:"",
      contractCurrentVersion:[],
    };
  },
  methods: {
    /**获取当前版次 */
    getCurrentVersion() {
      this.getContractCurrentVersionLoading = true
      getContractCurrentVersion(this.currentVersionQuery).then(response => {
        const data = response.data
        const versionStatus = []
        for(let i in data) {
          versionStatus[i] = {value:data[i].cx,label:data[i].cx}
        }
        versionStatus.push({value:"不选择版次", label:"不选择版次"})
        this.contractCurrentVersion = versionStatus
        this.getContractCurrentVersionLoading = false
      });
    },
    getDetail() {
        this.$modal.loading("数据加载数据，请稍等...")
        getContractNewVersionAddDetail(this.queryParams).then(response => {
           const data = response.data[0]
           this.formParams = {
              ...this.formParams,
              s1:data.sdate,
              s2:data.edate,
              sdate: data.s2
           } 
           this.date1 = [data.sdate, data.edate]
           this.$modal.closeLoading()
        }).catch(()=> {
            this.$modal.closeLoading()
        })
    },
    createContractNewVersion() {
      this.$modal.loading("数据正在添加，请稍等...")
      contractNewVersionCreate(this.formParams).then(response => {
        this.$modal.closeLoading()
        this.close()
        this.$emit('refresh', true)
      }).catch(()=> {
        this.$modal.closeLoading()
      })
    },
    /**添加合同 */
    handleCreate(formName) {
      this.$refs[formName].validate((valid, data) => {
        if(valid) {
          const sdate = this.formParams.sdate
          const s1 = this.formParams.s1
          const e1 = this.formParams.e1
          const e2 = this.formParams.e2
          const edate = this.formParams.edate
          this.formParams.count1 = 0
          const count1 = this.formParams.count1
          if (sdate && (s1 >= sdate || e1 >= sdate)) return this.$modal.msgError("选择时间在不可选择时间")
          if (sdate && edate && (sdate > edate)) return this.$modal.msgError("结束时间大于开始时间")
          if (sdate && edate && (e2 > sdate) && count1 == 0) {
            return this.$modal.confirm("是否提前结束当前版本已开发新版本？").then(()=> {
              this.formParams.count1 = 1
              this.createContractNewVersion()
            })
          }
          if (sdate && edate && (s2 < sdate)) {
            return this.$modal.confirm("是否添加新版本？").then(() => {
              this.createContractNewVersion()
            }).catch(() => {})
          }      
        } else {
          for(let i in data) {
            const item = data[i]
            for(let j in item) {
              const message = item[j].message
              return this.$modal.msgError(message)
            }
          }
          return false
        }
      })
    },
    /**改变窗口状态 */
    changeContractNewAdd() {
      this.openContractNewAdd = !this.openContractNewAdd;
    },
    /**重置数据 */
    reset(vm) {
      //Object.assign(vm.$data, vm.$options.data())
    },
    /**关闭弹窗 */
    beforeClose(done) {
      this.close()
      done()
    },
    /**关闭弹窗 */
    close() {
      this.changeContractNewAdd()
      this.reset(this)
    },
    /**打开弹窗 */
    open(data) {
      this.changeContractNewAdd()
      this.formParams = {
          ...this.formParams,
          csuid:data.uid,
          cx0:data.cx,
          cxx:data.cx,
          e1:data.sdate,
          e2:data.edate
      }
      this.queryParams = {
        csuid:data.uid,
        cx:data.cx,
        s1:data.sdate,
        s2:data.edate
      }
      this.currentVersionQuery = {
        csuid:data.uid
      }
      this.date2 = [data.sdate, data.edate]
      this.getDetail()
      this.getCurrentVersion()
    }
  }
};
</script>
